unit pcneOpcoes;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  TForOpcoes = class(TForm)
    gridOpcoes: TStringGrid;
    procedure gridOpcoesKeyPress(Sender: TObject; var Key: Char);
    procedure posiciona;
    procedure FormShow(Sender: TObject);
    procedure gridOpcoesDblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    posicao: integer;
  end;

var
  ForOpcoes: TForOpcoes;

implementation

{$R *.DFM}

procedure TForOpcoes.gridOpcoesKeyPress(Sender: TObject; var Key: Char);
var
  R1, R2, R3: integer;
begin
  if (Key = #13) or (Key = #27) then
  begin
    ForOpcoes.Close;
    exit;
  end;
  if (Key = #32) then
  begin
    if gridOpcoes.Row = gridOpcoes.RowCount - 1 then
    begin
      gridOpcoes.Row := 0;
      exit;
    end;
    gridOpcoes.Row := gridOpcoes.Row + 1;
    exit;
  end;
  R1 := gridOpcoes.Row;
  R3 := 0;
  while (R1 < gridOpcoes.RowCount - 1) do
  begin
    inc(R1);
    if UpperCase(Copy(gridOpcoes.Cells[0, R1], 1, 1)) = UpperCase(Key) then
    begin
      gridOpcoes.Row := R1;
      R3 := 1;
      Break;
    end;
  end;
  if R3 = 1 then
    exit;
  R2 := R1;
  R1 := -1;
  while (R1 < R2) do
  begin
    inc(R1);
    if UpperCase(Copy(gridOpcoes.Cells[0, R1], 1, 1)) = UpperCase(Key) then
    begin
      gridOpcoes.Row := R1;
      Break;
    end;
  end;
end;

procedure TForOpcoes.posiciona;
begin
  gridOpcoes.row := posicao;
end;

procedure TForOpcoes.FormShow(Sender: TObject);
begin
  posiciona;
end;

procedure TForOpcoes.gridOpcoesDblClick(Sender: TObject);
begin
  ForOpcoes.Close;
end;

end.

